const baseURL = 'https://tool.needyoutd.com/api/';
export const myRequesy = (options) => {
  return new Promise((resolve, reject) => {
    wx.request({
      url: baseURL + options.url,
      method: options.method || 'GET',
      data: options.data || {},
      timeout: 15000,
      header: {
        Authorization: wx.getStorageSync('userInfo').token || ''
      },
      success: (res) => {
        if (res.data.code === 0) {
          resolve(res.data)
        } else if (res.data.code === 2001 || res.data.code === 2002 || res.data.code === 2003) {
          wx.removeStorage({
            key: 'userInfo',
          })
        }

        if (options.url == 'update-auth' || options.url == 'tianxie' || options.url == 'Login') return
        if (!wx.getStorageSync('userInfo').token) {
          wx.switchTab({
            url: '/pages/index/index',
          })
        }
      },
      fail: (err) => {
        wx.showLoading({
          title: '网络开小差了，请稍后重试',
          mask: true
        })
        reject(err)
      }
    })
  })
}